require(["config"], function() {
    require(["jquery", "template", "header", "cookie"], function($, template) {

        function Cart() {
            this.products = null;
            this.load();
            this.addListener();
        }

        Cart.prototype = {
            constructor: Cart,
            // 加载，渲染页面
            load: function() {
                $.cookie.json = true;
                var products = this.products = $.cookie("cart") || [];
                if (products.length === 0) { // 购物车为空
                    $(".cart_empty").show().siblings(".cart_noempty").hide();
                    return;
                }
                $(".cart_empty").hide().siblings(".cart_noempty").removeClass("hidden");
                // 渲染模板
                var data = {products: products}
                var html = template("cart-body-template", data);
                $(".cart-table tbody").html(html);
            },
            // 注册事件监听
            addListener: function() {
                $(".cart-table tbody").on("click", ".del", $.proxy(this.delHandler, this))
                $(".cart-table tbody").on("click", ".decrement,.increment", $.proxy(this.decIncHandler, this))
                $(".cart-table tbody").on("blur", ".amount", $.proxy(this.inputHandler, this));
                $(".quan").on("click", $.proxy(this.ckAllHandler, this));
                $(".cart-table tbody").on("click", ".quan1", $.proxy(this.ckProdHandler, this));
            },
            // 删除单行
            delHandler: function(event) {
                var src = event.target;
                var tr = $(src).parents("tr");
                var id = tr.find(".id").text();
                this.products = this.products.filter(function(prod) {
                    return !(prod.id == id);
                });
                $.cookie("cart", this.products, {expires: 10, path: "/"});
                tr.remove();
                this.calcTotal();
                // 判断是否有购物车商品
                if (this.products.length === 0) {
                    $(".cart_empty").show().siblings(".cart_noempty").hide();
                }
            },
            // 数量处理
            decIncHandler: function(event) {
                var src = event.target;
                var tr = $(src).parents("tr");
                var id = tr.find(".id").text();
                var product = this.products.filter(function(prod) {
                    return prod.id == id;
                })[0];
                if ($(src).is(".decrement")) {
                    if (product.amount <= 1)
                        return;
                    product.amount--;
                } else {
                    product.amount++;
                }

                $.cookie("cart", this.products, {expires:10, path:"/"});

                tr.find(".amount").val(product.amount);
                tr.find(".sub").text(product.amount*product.price);
                this.calcTotal();
            },
            // 输入数量处理
            inputHandler: function(event) {
                var src = event.target;
                var tr = $(src).parents("tr");
                var id = tr.find(".id").text();
                var product = this.products.filter(prod=>prod.id == id)[0];
                var _amount = $(src).val();
                var reg = /^[1-9]\d*$/;
                if (!reg.test(_amount)) {
                    $(src).val(product.amount);
                    return;
                }
                product.amount = _amount;

                $.cookie("cart", this.products, {expires:10, path:"/"});

                tr.find(".sub").text(product.amount * product.price);
                this.calcTotal();
            },
            // 全选处理
            ckAllHandler: function(event) {
                var status = $(event.target).prop("checked") // element.checked
                console.log(status);
                $(".quan1").prop("checked", status);
                this.calcTotal();
            },
            // 部分选中处理
            ckProdHandler: function(event) {
                var count = $(".quan1:checked").length;
                var status = count === this.products.length;
                $(".quan").prop("checked", status);
                this.calcTotal();
            },
            // 计算合计金额
            calcTotal: function() {
                var sum = 0;
                $(".quan1:checked").each(function(index, element) {
                    sum += Number($(this).parents("tr").find(".sub").text());
                });
                $(".shu").text(sum);
            }
        }

        new Cart();
    });
});